package sample.test.cc.cool.gui.client;

import java.awt.Dimension;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;
import java.util.Set;

import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JSeparator;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.WindowConstants;

import sample.test.cc.cool.gui.controllers.AdmController;
import sample.test.cc.cool.gui.controllers.ClientController;
import sample.test.cc.cool.gui.util.Containable;
import BESA.SystemLevel.AdmBESA;
import BESA.SystemLevel.transactions.agent.Intention;

public class GuiClient extends JFrame implements Containable {

	private static final long serialVersionUID = 1L;
	private JPanel jContentPane = null;
	private JLabel jLabel = null;
	private JLabel ipLabel = null;
	private JLabel jLabel1 = null;
	private JLabel multicastLabel = null;
	private JLabel jLabel2 = null;
	private JLabel portMCLabel = null;
	private JSeparator panelSeparator = null; // @jve:decl-index=0:visual-constraint="765,41"
	private JLabel jLabel3 = null;
	private JLabel Events = null;
	private JLabel jLabel4 = null;
	private JSeparator panelSeparator1 = null;
	private JSeparator panelSeparator11 = null;
	private JComboBox agentComboBox = null;
	private JLabel jLabel5 = null;
	private JComboBox transactionsEventComboBox = null;
	private JLabel jLabel6 = null;
	private JRadioButton readRadioButton = null;
	private JRadioButton writeRadioButton = null;
	private ButtonGroup operationGroup = null; // @jve:decl-index=0:
	private JLabel jLabel7 = null;
	private JTextField valueNewField = null;
	private ActionListener operationListener = null; // @jve:decl-index=0:
	private JButton sendEventButton = null;
	private JButton openTransactionButton = null;
	private JSeparator panelSeparator12 = null;
	private JComboBox trasactionsComboBox = null;
	private JLabel jLabel8 = null;
	private JButton closeTransactionButton = null;

	/**
	 * This is the default constructor
	 */
	public GuiClient() {
		super();
		initialize();
		if (AdmController.existContainner) {
			updateContainnerMetaData();
		}
	}

	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	private void initialize() {
		this.setSize(593, 349);
		this.setResizable(false);
		this.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
		this.setContentPane(getJContentPane());
		this.setTitle("eBESA -");

	}

	/**
	 * This method initializes jContentPane
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getJContentPane() {
		if (jContentPane == null) {
			jLabel8 = new JLabel();
			jLabel8.setBounds(new Rectangle(20, 190, 71, 21));
			jLabel8.setHorizontalAlignment(SwingConstants.RIGHT);
			jLabel8.setText("Transaction:");
			operationListener = new OperationListenner();
			jLabel7 = new JLabel();
			jLabel7.setBounds(new Rectangle(320, 230, 71, 21));
			jLabel7.setHorizontalAlignment(SwingConstants.RIGHT);
			jLabel7.setText("Value:");
			jLabel6 = new JLabel();
			jLabel6.setBounds(new Rectangle(310, 190, 81, 21));
			jLabel6.setHorizontalAlignment(SwingConstants.RIGHT);
			jLabel6.setText("Operation:");
			jLabel5 = new JLabel();
			jLabel5.setBounds(new Rectangle(310, 150, 81, 21));
			jLabel5.setHorizontalAlignment(SwingConstants.RIGHT);
			jLabel5.setText("Transaction:");
			jLabel4 = new JLabel();
			jLabel4.setBounds(new Rectangle(340, 110, 51, 21));
			jLabel4.setHorizontalAlignment(SwingConstants.RIGHT);
			jLabel4.setText("Agent:");
			Events = new JLabel();
			Events.setBounds(new Rectangle(310, 60, 71, 21));
			Events.setText("Events");
			jLabel3 = new JLabel();
			jLabel3.setBounds(new Rectangle(10, 60, 91, 21));
			jLabel3.setText("Transactions");
			portMCLabel = new JLabel();
			portMCLabel.setBounds(new Rectangle(500, 20, 51, 21));
			portMCLabel.setHorizontalAlignment(SwingConstants.CENTER);
			portMCLabel.setText("-");
			jLabel2 = new JLabel();
			jLabel2.setBounds(new Rectangle(400, 20, 91, 21));
			jLabel2.setText("Multicast port:");
			multicastLabel = new JLabel();
			multicastLabel.setBounds(new Rectangle(280, 20, 101, 21));
			multicastLabel.setHorizontalAlignment(SwingConstants.CENTER);
			multicastLabel.setText("-");
			jLabel1 = new JLabel();
			jLabel1.setBounds(new Rectangle(160, 20, 111, 21));
			jLabel1.setHorizontalAlignment(SwingConstants.RIGHT);
			jLabel1.setText("Multicast address:");
			ipLabel = new JLabel();
			ipLabel.setBounds(new Rectangle(40, 20, 101, 21));
			ipLabel.setHorizontalAlignment(SwingConstants.CENTER);
			ipLabel.setText("-");
			jLabel = new JLabel();
			jLabel.setBounds(new Rectangle(10, 20, 21, 21));
			jLabel.setHorizontalAlignment(SwingConstants.RIGHT);
			jLabel.setText("IP:");
			panelSeparator = new JSeparator(JSeparator.VERTICAL);
			panelSeparator.setSize(new Dimension(8, 226));
			panelSeparator.setLocation(new Point(285, 60));
			jContentPane = new JPanel();
			jContentPane.setLayout(null);
			jContentPane.add(panelSeparator, null);
			jContentPane.add(jLabel, null);
			jContentPane.add(ipLabel, null);
			jContentPane.add(jLabel1, null);
			jContentPane.add(multicastLabel, null);
			jContentPane.add(jLabel2, null);
			jContentPane.add(portMCLabel, null);
			jContentPane.add(jLabel3, null);
			jContentPane.add(Events, null);
			jContentPane.add(jLabel4, null);
			jContentPane.add(getPanelSeparator1(), null);
			jContentPane.add(getPanelSeparator11(), null);
			jContentPane.add(getAgentComboBox(), null);
			jContentPane.add(jLabel5, null);
			jContentPane.add(getTransactionsEventComboBox(), null);
			jContentPane.add(jLabel6, null);
			jContentPane.add(getReadRadioButton(), null);
			jContentPane.add(getWriteRadioButton(), null);
			jContentPane.add(jLabel7, null);
			jContentPane.add(getValueNewField(), null);
			jContentPane.add(getSendEventButton(), null);
			jContentPane.add(getOpenTransactionButton(), null);
			jContentPane.add(getPanelSeparator12(), null);
			jContentPane.add(getTrasactionsComboBox(), null);
			jContentPane.add(jLabel8, null);
			jContentPane.add(getCloseTransactionButton(), null);
		}
		return jContentPane;
	}

	@Override
	public void updateContainnerMetaData() {
		multicastLabel.setText(AdmBESA.getInstance().getMulticastInetAddr()
				.toString().substring(1));
		ipLabel.setText(AdmBESA.getInstance().getIP());
		portMCLabel.setText(AdmBESA.getInstance().getMulticastPort() + "");
		this.setTitle("eBESA - " + AdmBESA.getInstance().getAlias());
	}

	public void updateAgentList(Set<String> agentsAlias) {
		getAgentComboBox().removeAll();
		for (String s : agentsAlias) {
			getAgentComboBox().addItem(s);
		}

	}

	/**
	 * This method initializes panelSeparator1
	 * 
	 * @return javax.swing.JSeparator
	 */
	private JSeparator getPanelSeparator1() {
		if (panelSeparator1 == null) {
			panelSeparator1 = new JSeparator(JSeparator.HORIZONTAL);
			panelSeparator1.setBounds(new Rectangle(10, 90, 261, 11));
		}
		return panelSeparator1;
	}

	/**
	 * This method initializes panelSeparator11
	 * 
	 * @return javax.swing.JSeparator
	 */
	private JSeparator getPanelSeparator11() {
		if (panelSeparator11 == null) {
			panelSeparator11 = new JSeparator(JSeparator.HORIZONTAL);
			panelSeparator11.setBounds(new Rectangle(310, 90, 251, 10));
		}
		return panelSeparator11;
	}

	/**
	 * This method initializes agentComboBox
	 * 
	 * @return javax.swing.JComboBox
	 */
	private JComboBox getAgentComboBox() {
		if (agentComboBox == null) {
			agentComboBox = new JComboBox();
			agentComboBox.setBounds(new Rectangle(410, 110, 141, 21));
		}
		return agentComboBox;
	}

	/**
	 * This method initializes transactionsEventComboBox
	 * 
	 * @return javax.swing.JComboBox
	 */
	private JComboBox getTransactionsEventComboBox() {
		if (transactionsEventComboBox == null) {
			transactionsEventComboBox = new JComboBox();
			transactionsEventComboBox
					.setBounds(new Rectangle(410, 150, 141, 21));
		}
		return transactionsEventComboBox;
	}

	/**
	 * This method initializes readRadioButton
	 * 
	 * @return javax.swing.JRadioButton
	 */
	private JRadioButton getReadRadioButton() {
		if (readRadioButton == null) {
			readRadioButton = new JRadioButton();
			readRadioButton.setBounds(new Rectangle(410, 190, 61, 21));
			readRadioButton.addActionListener(operationListener);
			readRadioButton.setSelected(true);
			getOperationGroup().add(readRadioButton);
			readRadioButton.setText("Read");
		}
		return readRadioButton;
	}

	/**
	 * This method initializes writeRadioButton
	 * 
	 * @return javax.swing.JRadioButton
	 */
	private JRadioButton getWriteRadioButton() {
		if (writeRadioButton == null) {
			writeRadioButton = new JRadioButton();
			writeRadioButton.setBounds(new Rectangle(480, 190, 61, 21));
			writeRadioButton.addActionListener(operationListener);
			getOperationGroup().add(writeRadioButton);
			writeRadioButton.setText("Write");
		}
		return writeRadioButton;
	}

	private ButtonGroup getOperationGroup() {
		if (operationGroup == null) {
			operationGroup = new ButtonGroup();
		}
		return operationGroup;
	}

	/**
	 * This method initializes valueNewField
	 * 
	 * @return javax.swing.JTextField
	 */
	private JTextField getValueNewField() {
		if (valueNewField == null) {
			valueNewField = new JTextField();
			valueNewField.setEditable(false);
			valueNewField.setBounds(new Rectangle(410, 230, 141, 21));
		}
		return valueNewField;
	}

	class OperationListenner implements ActionListener {

		@Override
		public void actionPerformed(ActionEvent e) {
			// if (e instanceof ) {
			String event = ((JRadioButton) e.getSource()).getText();
			System.out.println("**" + event);
			if (event.equals("Read")) {
				valueNewField.setEditable(false);
			} else if (event.equals("Write")) {
				valueNewField.setEditable(true);
			}

			// }
		}

	}

	/**
	 * This method initializes sendEventButton
	 * 
	 * @return javax.swing.JButton
	 */
	private JButton getSendEventButton() {
		if (sendEventButton == null) {
			sendEventButton = new JButton();
			sendEventButton.setBounds(new Rectangle(450, 270, 101, 21));
			sendEventButton.setText("Send Event");
			sendEventButton
					.addActionListener(new java.awt.event.ActionListener() {
						public void actionPerformed(java.awt.event.ActionEvent e) {
							System.err.println("___GUI");
							try {
								String alias = (String) agentComboBox
										.getSelectedItem();
								Long transaction = Long
										.parseLong((String) transactionsEventComboBox
												.getSelectedItem());
								if (readRadioButton.isSelected())
									ClientController.getInstance().sendEvent(
											alias, transaction, Intention.READ);
								else {
									int value = Integer.parseInt(valueNewField
											.getText());
									ClientController.getInstance().sendEvent(
											alias, transaction,
											Intention.WRITE, value);
								}

							} catch (Exception e1) {
								showErrorMessage(e1.getMessage());
							}
						}
					});
		}
		return sendEventButton;
	}

	public void showErrorMessage(String message) {
		JOptionPane.showMessageDialog(this, message, "eBESA - Error",
				JOptionPane.ERROR_MESSAGE);
	}

	/**
	 * This method initializes openTransactionButton
	 * 
	 * @return javax.swing.JButton
	 */
	private JButton getOpenTransactionButton() {
		if (openTransactionButton == null) {
			openTransactionButton = new JButton();
			openTransactionButton.setBounds(new Rectangle(70, 120, 161, 21));
			openTransactionButton.setText("Open Transaction");
			openTransactionButton
					.addActionListener(new java.awt.event.ActionListener() {
						public void actionPerformed(java.awt.event.ActionEvent e) {
							ClientController.getInstance().openTransaction();
						}
					});
		}
		return openTransactionButton;
	}

	/**
	 * This method initializes panelSeparator12
	 * 
	 * @return javax.swing.JSeparator
	 */
	private JSeparator getPanelSeparator12() {
		if (panelSeparator12 == null) {
			panelSeparator12 = new JSeparator(JSeparator.HORIZONTAL);
			panelSeparator12.setBounds(new Rectangle(15, 165, 256, 12));
		}
		return panelSeparator12;
	}

	/**
	 * This method initializes trasactionsComboBox
	 * 
	 * @return javax.swing.JComboBox
	 */
	private JComboBox getTrasactionsComboBox() {
		if (trasactionsComboBox == null) {
			trasactionsComboBox = new JComboBox();
			trasactionsComboBox.setBounds(new Rectangle(110, 190, 141, 21));
		}
		return trasactionsComboBox;
	}

	/**
	 * This method initializes closeTransactionButton
	 * 
	 * @return javax.swing.JButton
	 */
	private JButton getCloseTransactionButton() {
		if (closeTransactionButton == null) {
			closeTransactionButton = new JButton();
			closeTransactionButton.setBounds(new Rectangle(70, 240, 161, 21));
			closeTransactionButton.setText("Close Transaction");
			closeTransactionButton
					.addActionListener(new java.awt.event.ActionListener() {
						public void actionPerformed(java.awt.event.ActionEvent e) {
							if (getTrasactionsComboBox().getSelectedIndex() != -1){
								long sel = Long.parseLong((String) getTrasactionsComboBox().getSelectedItem());
								if(ClientController.getInstance().closeTransaction(sel)){
									showOkMessage("The transaction "+sel+" has been comleted succefull");
								}else{
									showErrorMessage("The transaction "+sel+" can not be completed due  a conflicted with another transaction.");
								}
							}
						}
					});
		}
		return closeTransactionButton;
	}

	protected void showOkMessage(String string) {
		JOptionPane.showMessageDialog(this, string, "eBESA - Ok",
				JOptionPane.OK_CANCEL_OPTION);
		
	}

	public void updateTransactionComboBoxes(List<Long> activeTransaction) {
		getTransactionsEventComboBox().removeAllItems();
		getTrasactionsComboBox().removeAllItems();
		for (Long l : activeTransaction) {
			getTransactionsEventComboBox().addItem(l + "");
			getTrasactionsComboBox().addItem(l + "");
		}

	}
} // @jve:decl-index=0:visual-constraint="10,10"

